package com.sskj.trafficcontrol.util;
import com.sskj.trafficcontrol.model.vo.TodayBean.WeatherIdBean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.trafficcontrol.model.vo.FutureVO;
import com.sskj.trafficcontrol.model.vo.TodayBean;
import com.sskj.trafficcontrol.model.vo.TotalDtailVO;
import com.sskj.trafficcontrol.model.vo.WeatherVO;
import com.sskj.trafficcontrol.util.weather.Weather;
import org.springframework.beans.BeanUtils;

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;


/**
 * 根据城市名/id查询天气
 *
 * @author silk
 */
public class CityWeatherUtil {
    /**
     * 根据城市名获取
     *
     * @param cityName
     * @return
     */
    public static String excute(String cityName) {
        String url =//此处以返回json格式数据示例,所以format=2,以根据城市名称为例,cityName传入中文
                "http://v.juhe.cn/weather/index?cityname=" + cityName + "&key=ca6783d3c6642075d067d83d3cb694ae";
        return PureNetUtil.get(url);//通过工具类获取返回数据
    }

    /**
     * 获取返回数据中的一个属性示例,此处以获取今日温度为例
     * "temperature": "8℃~20℃"     今日温度
     *
     * @return
     */
    public static void GetTodayTemperatureByCity(TotalDtailVO totalDtailVO, String city) {
        String result = excute(city);
        if (result != null) {
            JSONObject jsonObject = JSONObject.parseObject(result, JSONObject.class);
            WeatherVO obj = JSONObject.parseObject(result, WeatherVO.class);

            TodayBean todayBean = new TodayBean();
            BeanUtils.copyProperties(obj.getResult().getToday(),todayBean);
            totalDtailVO.setToday(todayBean);

            JSONObject jsonFuture = JSONObject.parseObject(obj.getResult().getFuture(), JSONObject.class);

//            FutureVO futureVO1 =  JSONObject.parseObject(jsonFuture.get("day_"+getFutureJsonDateKey(0)).toString(), FutureVO.class);

            FutureVO futureVO2 = JSONObject.parseObject(jsonFuture.get("day_" + getFutureJsonDateKey(1)).toString(), FutureVO.class);
            futureVO2.setDate(futureVO2.getDate().substring(0,4)+"-"+futureVO2.getDate().substring(4,6)+"-"+futureVO2.getDate().substring(6,8));
            FutureVO futureVO3 = JSONObject.parseObject(jsonFuture.get("day_" + getFutureJsonDateKey(2)).toString(), FutureVO.class);
            futureVO3.setDate(futureVO3.getDate().substring(0,4)+"-"+futureVO3.getDate().substring(4,6)+"-"+futureVO3.getDate().substring(6,8));
            FutureVO futureVO4 = JSONObject.parseObject(jsonFuture.get("day_" + getFutureJsonDateKey(3)).toString(), FutureVO.class);
            futureVO4.setDate(futureVO4.getDate().substring(0,4)+"-"+futureVO4.getDate().substring(4,6)+"-"+futureVO4.getDate().substring(6,8));
            totalDtailVO.getFutures().add(futureVO2);
            totalDtailVO.getFutures().add(futureVO3);
            totalDtailVO.getFutures().add(futureVO4);
        }
    }

    public static void main(String[] args) {
//        TotalDtailVO totalDtailVO = new TotalDtailVO();
//        GetTodayTemperatureByCity(totalDtailVO,"北京市");
//

    }


    private static String getFutureJsonDateKey(int days) {
        if (days == 0) {
            LocalDate localDate = LocalDate.now();
            return localDate.toString().replace("-", "");
        } else if (days == 1) {
            LocalDate localDate = LocalDate.now();
            return localDate.plusDays(1).toString().replace("-", "");
        } else if (days == 2) {
            LocalDate localDate = LocalDate.now();
            return localDate.plusDays(2).toString().replace("-", "");
        } else if (days == 3) {
            LocalDate localDate = LocalDate.now();
            return localDate.plusDays(3).toString().replace("-", "");
        } else {
            return "";
        }
    }


    private static String getFutureJsonDate(int days) {
        if (days == 0) {
            LocalDate localDate = LocalDate.now();
            String[] nums=  localDate.toString().split("-");
            return  nums[0]+"年"+nums[1]+"月"+ nums[2]+"日";
        } else if (days == 1) {
            LocalDate localDate = LocalDate.now();
            String[] nums= localDate.plusDays(1).toString().split("-");
            return  nums[0]+"年"+nums[1]+"月"+ nums[2]+"日";
        } else if (days == 2) {
            LocalDate localDate = LocalDate.now();
            String[] nums= localDate.plusDays(2).toString().split("-");
            return  nums[0]+"年"+nums[1]+"月"+ nums[2]+"日";
        } else if (days == 3) {
            LocalDate localDate = LocalDate.now();
            String[] nums= localDate.plusDays(3).toString().split("-");
            return  nums[0]+"年"+nums[1]+"月"+ nums[2]+"日";
        } else {
            return "";
        }
    }

    private static String getFutureJsonDate1(int days) {
        if (days == 0) {
            LocalDate localDate = LocalDate.now();
            return  localDate.toString();
        } else if (days == 1) {
            LocalDate localDate = LocalDate.now();
            return  localDate.plusDays(1).toString();
        } else if (days == 2) {
            LocalDate localDate = LocalDate.now();
            return localDate.plusDays(2).toString();
        } else if (days == 3) {
            LocalDate localDate = LocalDate.now();
            return localDate.plusDays(3).toString();
        } else {
            return "";
        }
    }

    private static String getWeek() {
            LocalDate localDate = LocalDate.now();
            return localDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
    }

    public static String getNumric(String str) {
        String str2 = "";
        if (str != null && !"".equals(str)) {
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                    str2 += str.charAt(i);
                }
            }
        }
        return str2;
    }

    public static void GetTodayWeatherByCity(TotalDtailVO totalDtailVO, Weather weather) {
        if(weather!=null){
            TodayBean todayBean = new TodayBean();
            todayBean.setCity(weather.getCity());
            todayBean.setDate_y(getFutureJsonDate(0));
            todayBean.setWeek(getWeek());
            todayBean.setTemperature((weather.getForecast().getWeather().get(0).getLow().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(0).getLow()):getNumric(weather.getForecast().getWeather().get(0).getLow()))+"℃～"+(weather.getForecast().getWeather().get(0).getHigh().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(0).getHigh()):getNumric(weather.getForecast().getWeather().get(0).getHigh()))+"℃");
            todayBean.setWeather(weather.getForecast().getWeather().get(0).getDay().getType());
            todayBean.setWeather_id(new WeatherIdBean());
            todayBean.setWind(weather.getForecast().getWeather().get(0).getDay().getFengxiang()+weather.getForecast().getWeather().get(0).getDay().getFengli());
            todayBean.setTime(weather.getUpdatetime());

            totalDtailVO.setToday(todayBean);

            FutureVO futureVO2 = new FutureVO();
            futureVO2.setDate(getFutureJsonDate1(1));
            futureVO2.setWeather(weather.getForecast().getWeather().get(1).getDay().getType());
            futureVO2.setTemperature((weather.getForecast().getWeather().get(1).getLow().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(1).getLow()):getNumric(weather.getForecast().getWeather().get(1).getLow()))+"℃～"+(weather.getForecast().getWeather().get(1).getHigh().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(1).getHigh()):getNumric(weather.getForecast().getWeather().get(1).getHigh()))+"℃");
            futureVO2.setWind(weather.getForecast().getWeather().get(1).getDay().getFengxiang()+weather.getForecast().getWeather().get(1).getDay().getFengli());
            FutureVO futureVO3 = new FutureVO();
            futureVO3.setDate(getFutureJsonDate1(2));
            futureVO3.setWeather(weather.getForecast().getWeather().get(2).getDay().getType());
            futureVO3.setTemperature((weather.getForecast().getWeather().get(2).getLow().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(2).getLow()):getNumric(weather.getForecast().getWeather().get(2).getLow()))+"℃～"+(weather.getForecast().getWeather().get(2).getHigh().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(2).getHigh()):getNumric(weather.getForecast().getWeather().get(2).getHigh()))+"℃");
            futureVO3.setWind(weather.getForecast().getWeather().get(2).getDay().getFengxiang()+weather.getForecast().getWeather().get(2).getDay().getFengli());
            FutureVO futureVO4 = new FutureVO();
            futureVO4.setDate(getFutureJsonDate1(3));
            futureVO4.setWeather(weather.getForecast().getWeather().get(3).getDay().getType());
            futureVO4.setTemperature((getNumric(weather.getForecast().getWeather().get(3).getLow().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(3).getLow()):getNumric(weather.getForecast().getWeather().get(3).getLow()))+"℃～"+(weather.getForecast().getWeather().get(3).getHigh().contains("-")?"-"+getNumric(weather.getForecast().getWeather().get(3).getHigh()):getNumric(weather.getForecast().getWeather().get(3).getHigh())))+"℃");
            futureVO4.setWind(weather.getForecast().getWeather().get(3).getDay().getFengxiang()+weather.getForecast().getWeather().get(3).getDay().getFengli());
            totalDtailVO.getFutures().add(futureVO2);
            totalDtailVO.getFutures().add(futureVO3);
            totalDtailVO.getFutures().add(futureVO4);
        }
    }
}
